{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from src import config, utils\n",
    "import logging\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.metrics import roc_auc_score\n",
    "%matplotlib inline\n",
    "import seaborn as sns\n",
    "from sklearn.model_selection import GridSearchCV, RandomizedSearchCV\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def scorer(estimator, X, y):\n",
    "    y_pred = estimator.predict_proba(X)[:, 1]\n",
    "    return roc_auc_score(y, y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:Will drop ['a_feature', 'UserInfo_270'] len=2\n"
     ]
    }
   ],
   "source": [
    "assert config.dataset == 'val'\n",
    "train = pd.read_csv(config.pj_root + 'data/' + config.dataset + '.csv', index_col='no')\n",
    "\n",
    "assert train.columns[0] == 'flag'\n",
    "a_feature = pd.read_csv(config.pj_root + 'data/a_feature.csv', index_col='no')\n",
    "train = train.join(a_feature)\n",
    "\n",
    "if len(config.drop_columns) != 0:\n",
    "    logging.warning('Will drop %s len=%d' % (str(config.drop_columns), len(config.drop_columns)))\n",
    "    train = train.drop(config.drop_columns, axis=1)\n",
    "\n",
    "if len(config.select_columns) != 0:\n",
    "    config.select_columns.insert(0, 'flag')  # use flag in training\n",
    "    logging.warning('Will select %s len=%d' % (str(config.select_columns), len(config.select_columns)))\n",
    "    train = train[train.columns[train.columns.isin(config.select_columns)]]\n",
    "else:\n",
    "    config.select_columns = list(train.columns)[1:]\n",
    "\n",
    "\n",
    "if config.use_basic_process:\n",
    "    train, col_func_map = utils.basic_process(train, has_flag=True)\n",
    "    utils.dump_to_data(col_func_map, 'col_func_map.pkl')\n",
    "\n",
    "X = train.values[:, 1:]\n",
    "Y = train.values[:, 0:1].reshape((-1,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import xgboost\n",
    "m1 =  xgboost.XGBClassifier(**dict(\n",
    "    max_depth=3,\n",
    "    n_estimators=5,\n",
    "    base_score=0.5,\n",
    "    learning_rate=0.1,\n",
    "    objective='rank:pairwise',\n",
    "#     min_child_weight=100,\n",
    "    subsample=1,\n",
    "))\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "m2 = RandomForestClassifier( **{'n_estimators': 50, 'min_samples_leaf': 5, 'max_depth': 7, 'criterion': 'entropy', 'max_features': 'sqrt'})\n",
    "\n",
    "stacker = xgboost.XGBClassifier(\n",
    "    objective='rank:pairwise',\n",
    "    n_estimators=3,\n",
    "    max_depth=2\n",
    ")\n",
    "\n",
    "class Ensemble(object):\n",
    "    def __init__(self, n_folds, stacker, base_models):\n",
    "        self.n_folds = n_folds\n",
    "        self.stacker = stacker\n",
    "        self.base_models = base_models\n",
    "    def fit_predict(self, X, y, T):\n",
    "        X = np.array(X)\n",
    "        y = np.array(y)\n",
    "        T = np.array(T)\n",
    "        folds = list(KFold(n_splits=self.n_folds, shuffle=True, random_state=2016).split(X))\n",
    "        S_train = np.zeros((X.shape[0], len(self.base_models)))\n",
    "        S_test = np.zeros((T.shape[0], len(self.base_models)))\n",
    "        for i, clf in enumerate(self.base_models):\n",
    "            S_test_i = np.zeros((T.shape[0], len(folds)))\n",
    "            for j, (train_idx, test_idx) in enumerate(folds):\n",
    "                X_train = X[train_idx]\n",
    "                y_train = y[train_idx]\n",
    "                X_holdout = X[test_idx]\n",
    "                clf.fit(X_train, y_train)\n",
    "                y_pred = clf.predict_proba(X_holdout)\n",
    "                y_pred = y_pred[:, 1]\n",
    "                print('Model %d/%d AUC: %f'  % (i+1, len(self.base_models), roc_auc_score(y[test_idx], y_pred)))\n",
    "                S_train[test_idx, i] = y_pred\n",
    "                S_test_i[:, j] = clf.predict(T)[:]\n",
    "            print('Model %d/%d ALL AUC:%f' % (i+1, len(self.base_models), roc_auc_score(y, S_train[:, i])))\n",
    "            S_test[:, i] = S_test_i.mean(1)\n",
    "        self.stacker.fit(S_train, y)\n",
    "        y_pred = self.stacker.predict_proba(S_test)[:]\n",
    "        return y_pred, S_train, S_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Use 4 Folds...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model 1/2 AUC: 0.530899\n",
      "Model 1/2 AUC: 0.522749\n",
      "Model 1/2 AUC: 0.466381\n",
      "Model 1/2 ALL AUC:0.497976\n",
      "Model 2/2 AUC: 0.555398\n",
      "Model 2/2 AUC: 0.501387\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Fold 1/4 Score: 0.518371 \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model 2/2 AUC: 0.551955\n",
      "Model 2/2 ALL AUC:0.520862\n",
      "Model 1/2 AUC: 0.557004\n",
      "Model 1/2 AUC: 0.545680\n",
      "Model 1/2 AUC: 0.597158\n",
      "Model 1/2 ALL AUC:0.553561\n",
      "Model 2/2 AUC: 0.587489\n",
      "Model 2/2 AUC: 0.575657\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Fold 2/4 Score: 0.518899 \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model 2/2 AUC: 0.592104\n",
      "Model 2/2 ALL AUC:0.572527\n",
      "Model 1/2 AUC: 0.507902\n",
      "Model 1/2 AUC: 0.610975\n",
      "Model 1/2 AUC: 0.545799\n",
      "Model 1/2 ALL AUC:0.546386\n",
      "Model 2/2 AUC: 0.520774\n",
      "Model 2/2 AUC: 0.553777\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Fold 3/4 Score: 0.580068 \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model 2/2 AUC: 0.513103\n",
      "Model 2/2 ALL AUC:0.528796\n",
      "Model 1/2 AUC: 0.509266\n",
      "Model 1/2 AUC: 0.622972\n",
      "Model 1/2 AUC: 0.557188\n",
      "Model 1/2 ALL AUC:0.558114\n",
      "Model 2/2 AUC: 0.533118\n",
      "Model 2/2 AUC: 0.593867\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Fold 4/4 Score: 0.559520 \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model 2/2 AUC: 0.557573\n",
      "Model 2/2 ALL AUC:0.552095\n"
     ]
    }
   ],
   "source": [
    "logging.info('Use %d Folds...' % config.kfold_k)\n",
    "kf = KFold(n_splits=config.kfold_k)\n",
    "all_score = 0\n",
    "for i, (train_index, test_index) in enumerate(kf.split(X)):\n",
    "    X_train, X_test = X[train_index], X[test_index]\n",
    "    y_train, y_test = Y[train_index], Y[test_index]\n",
    "    model = Ensemble(3, stacker, [m1, m2])\n",
    "    y_pred, _, __ = model.fit_predict(X_train, y_train, X_test)\n",
    "#     print(y_pred)\n",
    "    score = utils.report(y_test, y_pred[:, 1])\n",
    "    all_score += score\n",
    "    logging.info('Fold %d/%d Score: %f ' % (i + 1, config.kfold_k, score))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "kf = KFold(n_splits=config.kfold_k)\n",
    "all_score = 0\n",
    "for i, (train_index, test_index) in enumerate(kf.split(X)):\n",
    "    X_train, X_test = X[train_index], X[test_index]\n",
    "    y_train, y_test = Y[train_index], Y[test_index]\n",
    "    if i==1: break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model 1/2 AUC: 0.557004\n",
      "Model 1/2 AUC: 0.545680\n",
      "Model 1/2 AUC: 0.597158\n",
      "Model 1/2 ALL AUC:0.553561\n",
      "Model 2/2 AUC: 0.559944\n",
      "Model 2/2 AUC: 0.566251\n",
      "Model 2/2 AUC: 0.590783\n",
      "Model 2/2 ALL AUC:0.561781\n"
     ]
    }
   ],
   "source": [
    "model = Ensemble(3, stacker, [m1, m2])\n",
    "y_pred, S_train, S_test = model.fit_predict(X_train, y_train, X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.50444316416138546"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# stacker = xgboost.XGBClassifier(\n",
    "#     objective='rank:pairwise',\n",
    "#     n_estimators=300,\n",
    "#     max_depth=2,\n",
    "#     learning_rate=0.0001,\n",
    "# #     min_child_weight=10,\n",
    "# #     subsample=0.75\n",
    "# )\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "\n",
    "stacker = LogisticRegression(\n",
    "    C=0.1,\n",
    "    class_weight='balanced'\n",
    "    )\n",
    "S_train[:, 0] = (S_train[:, 0] - S_train[:, 0].mean()) / S_train[:, 0].std()\n",
    "S_train[:, 1] = (S_train[:, 1] - S_train[:, 1].mean()) / S_train[:, 1].std()\n",
    "S_test[:, 0] = (S_test[:, 0] - S_train[:, 0].mean()) / S_train[:, 0].std()\n",
    "S_test[:, 1] = (S_test[:, 1] - S_train[:, 1].mean()) / S_train[:, 1].std()\n",
    "\n",
    "# stacker.fit(S_train, y_train, eval_metric='auc', early_stopping_rounds=50, eval_set=[(S_test, y_test)])\n",
    "stacker.fit(S_train, y_train)\n",
    "roc_auc_score(y_test, stacker.predict_proba(S_test)[:, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = pd.DataFrame(S_train, columns=['m1', 'm2'])\n",
    "train= pd.concat([train, pd.DataFrame(y_train, columns=['flag'])], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>m1</th>\n",
       "      <th>m2</th>\n",
       "      <th>flag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.154077</td>\n",
       "      <td>1.633997</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.619334</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>-1.018088</td>\n",
       "      <td>0.454452</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>-0.221156</td>\n",
       "      <td>0.266760</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>0.311426</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>1.400801</td>\n",
       "      <td>0.714599</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>1.882460</td>\n",
       "      <td>2.656191</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.695857</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>108</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.106033</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>0.369700</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>126</th>\n",
       "      <td>-1.382882</td>\n",
       "      <td>1.403990</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.695857</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>-0.043514</td>\n",
       "      <td>0.770039</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>189</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.106033</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>194</th>\n",
       "      <td>1.505237</td>\n",
       "      <td>-0.366330</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>232</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>-1.494029</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>234</th>\n",
       "      <td>-0.717222</td>\n",
       "      <td>-0.006200</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>282</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.132538</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>283</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.106033</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300</th>\n",
       "      <td>-0.313118</td>\n",
       "      <td>-0.539950</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>301</th>\n",
       "      <td>0.260393</td>\n",
       "      <td>0.107796</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>306</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.695857</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>311</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.872320</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>328</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.179601</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>329</th>\n",
       "      <td>-0.361405</td>\n",
       "      <td>-0.397059</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>341</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>1.096068</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>344</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>-0.216387</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>350</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.695857</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>355</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>1.935591</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>360</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.250446</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2550</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.193272</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2579</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.179601</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2599</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>0.890915</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2626</th>\n",
       "      <td>-3.133138</td>\n",
       "      <td>-2.562152</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2631</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.752743</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2637</th>\n",
       "      <td>-0.043514</td>\n",
       "      <td>-1.863006</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2650</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.106033</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2657</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.131037</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2714</th>\n",
       "      <td>-1.661437</td>\n",
       "      <td>-0.248842</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2718</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>0.681403</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2725</th>\n",
       "      <td>-2.267877</td>\n",
       "      <td>-1.529900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2728</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>0.655118</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2737</th>\n",
       "      <td>0.150492</td>\n",
       "      <td>0.543623</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2740</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>1.091320</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2761</th>\n",
       "      <td>0.650216</td>\n",
       "      <td>1.174061</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2762</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>-0.083997</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2771</th>\n",
       "      <td>2.154077</td>\n",
       "      <td>1.666927</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2797</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>0.369700</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2800</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>0.663403</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2823</th>\n",
       "      <td>-0.336008</td>\n",
       "      <td>-0.432615</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2829</th>\n",
       "      <td>0.260393</td>\n",
       "      <td>3.433223</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2835</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>-0.054426</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2837</th>\n",
       "      <td>-0.361405</td>\n",
       "      <td>0.709466</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2853</th>\n",
       "      <td>2.154077</td>\n",
       "      <td>0.005242</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2871</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>0.106033</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2898</th>\n",
       "      <td>1.146005</td>\n",
       "      <td>1.343543</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2904</th>\n",
       "      <td>0.327587</td>\n",
       "      <td>-0.031299</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2961</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.659973</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2972</th>\n",
       "      <td>0.497672</td>\n",
       "      <td>0.562691</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2978</th>\n",
       "      <td>0.354817</td>\n",
       "      <td>0.414984</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>220 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            m1        m2  flag\n",
       "1     2.154077  1.633997   1.0\n",
       "54    0.497672  0.619334   1.0\n",
       "65   -1.018088  0.454452   1.0\n",
       "73   -0.221156  0.266760   1.0\n",
       "77    0.354817  0.311426   1.0\n",
       "85    1.400801  0.714599   1.0\n",
       "92    1.882460  2.656191   1.0\n",
       "97    0.497672  0.695857   1.0\n",
       "108   0.327587  0.106033   1.0\n",
       "123   0.354817  0.369700   1.0\n",
       "126  -1.382882  1.403990   1.0\n",
       "145   0.497672  0.695857   1.0\n",
       "149  -0.043514  0.770039   1.0\n",
       "189   0.327587  0.106033   1.0\n",
       "194   1.505237 -0.366330   1.0\n",
       "232   0.354817 -1.494029   1.0\n",
       "234  -0.717222 -0.006200   1.0\n",
       "282   0.327587  0.132538   1.0\n",
       "283   0.327587  0.106033   1.0\n",
       "300  -0.313118 -0.539950   1.0\n",
       "301   0.260393  0.107796   1.0\n",
       "306   0.497672  0.695857   1.0\n",
       "311   0.497672  0.872320   1.0\n",
       "328   0.327587  0.179601   1.0\n",
       "329  -0.361405 -0.397059   1.0\n",
       "341   0.354817  1.096068   1.0\n",
       "344   0.327587 -0.216387   1.0\n",
       "350   0.497672  0.695857   1.0\n",
       "355   0.327587  1.935591   1.0\n",
       "360   0.327587  0.250446   1.0\n",
       "...        ...       ...   ...\n",
       "2550  0.327587  0.193272   1.0\n",
       "2579  0.327587  0.179601   1.0\n",
       "2599  0.354817  0.890915   1.0\n",
       "2626 -3.133138 -2.562152   1.0\n",
       "2631  0.497672  0.752743   1.0\n",
       "2637 -0.043514 -1.863006   1.0\n",
       "2650  0.327587  0.106033   1.0\n",
       "2657  0.497672  0.131037   1.0\n",
       "2714 -1.661437 -0.248842   1.0\n",
       "2718  0.354817  0.681403   1.0\n",
       "2725 -2.267877 -1.529900   1.0\n",
       "2728  0.354817  0.655118   1.0\n",
       "2737  0.150492  0.543623   1.0\n",
       "2740  0.354817  1.091320   1.0\n",
       "2761  0.650216  1.174061   1.0\n",
       "2762  0.354817 -0.083997   1.0\n",
       "2771  2.154077  1.666927   1.0\n",
       "2797  0.354817  0.369700   1.0\n",
       "2800  0.354817  0.663403   1.0\n",
       "2823 -0.336008 -0.432615   1.0\n",
       "2829  0.260393  3.433223   1.0\n",
       "2835  0.327587 -0.054426   1.0\n",
       "2837 -0.361405  0.709466   1.0\n",
       "2853  2.154077  0.005242   1.0\n",
       "2871  0.327587  0.106033   1.0\n",
       "2898  1.146005  1.343543   1.0\n",
       "2904  0.327587 -0.031299   1.0\n",
       "2961  0.497672  0.659973   1.0\n",
       "2972  0.497672  0.562691   1.0\n",
       "2978  0.354817  0.414984   1.0\n",
       "\n",
       "[220 rows x 3 columns]"
      ]
     },
     "execution_count": 216,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[train.flag==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
